package com.loanly.controller;

import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.classic.*;
//import org.hibernate.Session;
 
import com.loanly.model.Loan;
import com.loanly.util.HibernateUtil;
 
public class LoanManager extends HibernateUtil {
 
    public Loan add(Loan loan) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
//        loan.setLoanName("121");
        session.save(loan);
        session.getTransaction().commit();
        return loan;
    }
    public Loan delete(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Loan loan = (Loan) session.load(Loan.class, id);
        if(null != loan) {
            session.delete(loan);
        }
        session.getTransaction().commit();
        return loan;
    }
 
    public List<Loan> list() {
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Loan> loans = null;
        try {
             
        	loans = (List<Loan>)session.createQuery("from Loan").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return loans;
    }

}